package com.yksj.healthtalk.ui.friend;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog.OnDilaogClickListener;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;



/**
* @ClassName: 朋友查找
* @author jack_tang
* @date 2013-1-18 按照昵称和多美号查找 10:24:12
 */
public class FriendSearchMainActivity extends BaseFragmentActivity implements OnClickListener, OnDilaogClickListener{
	
	private LinearLayout mSearchFriendLin;//找朋友
	private LinearLayout  mSearchAddressLin;//地址
	private LinearLayout mSearchZXing;//二维码
	private CustomerInfoEntity mCustomerInfoEntity;
	private EditText mDuoMeiNum;
	private LinearLayout mSearchContact;//搜索联系人
	private int search_type;//判断是从我的患者还是社交场进入搜索界面 0 我的患者 1社交场
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_search_main_layout);
		
		initWidget();
		initData();
		
	}
	
	/**
	* @Title: initData
	* @Description: 
	* @param 
	* @return void    
	* @throws
	 */
	private void initData() {
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText(R.string.sure);
		
		if (getIntent().hasExtra("mCustomerInfoEntity") ) {
			mCustomerInfoEntity = getIntent().getParcelableExtra("mCustomerInfoEntity");
		}else {
			mCustomerInfoEntity = new CustomerInfoEntity();
		}
		
	}
	
	/**
	* @Title: initWidget
	* @Description:
	* @param    
	* @return void    
	* @throws
	 */
	private void initWidget() {
		initTitle();
		mSearchFriendLin = (LinearLayout)findViewById(R.id.search_friend_lin);
		mSearchAddressLin = (LinearLayout)findViewById(R.id.search_address_lin);
		mSearchZXing = (LinearLayout)findViewById(R.id.search_zxing_lin);
		mSearchContact = (LinearLayout)findViewById(R.id.search_contact);
		mDuoMeiNum = (EditText)findViewById(R.id.duomei_num_et);
		
		titleLeftBtn.setOnClickListener(this);
		mSearchAddressLin.setOnClickListener(this);
		mSearchFriendLin.setOnClickListener(this);		
		mSearchZXing.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		mSearchContact.setOnClickListener(this);
		
		
		if (getIntent().hasExtra("search_type") && getIntent().getIntExtra("search_type", 0) == 1) {
			search_type = getIntent().getIntExtra("search_type", 0);
			if(SmartFoxClient.getLoginUserInfo().isDoctor()){
				titleTextV.setText("查找朋友");
			}else{
				titleTextV.setText("查找病友");
			}
		}else {
			if(SmartFoxClient.getLoginUserInfo().isDoctor()){
				titleTextV.setText("搜索患者");
			}else{
				titleTextV.setText("搜索病友");
			}
		}
		
	
	}

	@Override
	public void onClick(View v) {
			Intent intent ;
		switch (v.getId()) {
		case R.id.search_contact:// 看看谁在使用壹健康
			DoubleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "此项功能需要读取您的通讯录,您是否同意?", "放弃", "同意", this);
			break;
		case R.id.search_friend_lin://按条件搜索
			 intent = new Intent();
			intent.setClass(getApplicationContext(), FriendSearchAboutFriendActivity.class);
			intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
			intent.putExtra("search_type", search_type);
			startActivityForResult(intent, 0);
			break;
		case R.id.search_address_lin://按健康关注
			intent = new Intent();
			intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
			intent.setClass(getApplicationContext(), FriendSearchAboutExperienceActivity.class);
			intent.putExtra("search_type", search_type);
			startActivityForResult(intent, 0);
//			finish();
			break;
		case R.id.search_zxing_lin:
			intent = new Intent();
			intent.setClass(getApplicationContext(), FriendSearchAboutZxingActivity.class);
			intent.putExtra("type","0");// 0-社交场  1-医生馆 2 商户
			startActivity(intent);
			break;
		case R.id.title_back:
			setResult(-1);
			finish();
			break;
		case R.id.title_right2:
			String str = mDuoMeiNum.getText().toString();
			if (TextUtils.isEmpty(str)) {
				ToastUtil.showShort(this,"对不起多美号和昵称不能为空~");
				return;
			}
			intent = new Intent(getApplicationContext(),FriendSearchUI.class);
			intent.putExtra("duoMeiNum", mDuoMeiNum.getText().toString());
			if(getIntent().hasExtra("adapter_type"))//表示列表是用选项的
			intent.putExtra("adapter_type", getIntent().getStringExtra("adapter_type"));
			intent.putExtra("picID", getIntent().getStringExtra("picID"));
			startActivity(intent);
			break;
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 0 && data != null) {
			mCustomerInfoEntity = data.getParcelableExtra("mCustomerInfoEntity");
			Intent intent = new Intent(this, FriendMainActivity.class);
			intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
			setResult(0, intent);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	

	
	@Override
	public void onBackPressed() {
		if (getSupportFragmentManager().getBackStackEntryCount () == 1) {
			getSupportFragmentManager(). popBackStackImmediate();
		}else {
			setResult(-1);
			finish();
		}
	}

	@Override
	public void onDismiss(DialogFragment fragment) {
	}

	//看看谁在使用壹健康
	@Override
	public void onClick(DialogFragment fragment, View v) {
		Intent intent= new Intent(getApplicationContext(),FriendSearchListActivity.class);
		intent.putExtra("isContact", true);
		startActivity(intent);
	}
}
